def maximumSwap(num: int):
    s = list(str(num))
    n = len(s)
    maxIdx = n - 1
    idx1 = idx2 = -1
    for i in range(n - 1, -1, -1):
        if s[i] > s[maxIdx]:
            maxIdx = i
        elif s[i] < s[maxIdx]:
            idx1, idx2 = i, maxIdx
    if idx1 < 0:
        return num
    s[idx1], s[idx2] = s[idx2], s[idx1]
    return int(''.join(s))
    
    #参考：力扣官方题解


print(maximumSwap(2456))